close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com



PHP - Functions

PHP function is a piece of code that can be reused many times. It can take input as argument list and return value.

Advantage of PHP functions are code reusability, less code and easy to understand.

PHP provides us with two major types of functions:

  • Built-in functions : PHP provides us with huge collection of built-in library functions. These functions are already coded and stored in form of functions.
  • User Defined Functions : PHP allows us to create our own customized functions

  • Creating a Function

    A function will not execute automatically when a page loads and function will executed by a call to the function.

    Syntax : Create Function

      
    function functionName() {
      code to be executed;
    } 

    A function name must start with a letter or an underscore. Function names are NOT case-sensitive.

    Let's put these syntax into real use.

    Following is the example showing how to create a function.

    Example : Create Function

    
    <?php 
    function codealways() {
        echo "Code Always To Become Best Programmer.";
      }
      codealways(); // call the function
    ?>

    This will produce the following result −

    Result

    
     Code Always To Become Best Programmer.  

    Function Parameters or Arguments

    PHP gives you option to pass your parameters inside a function.

    You can pass as many as parameters your like.

    Syntax : Create Function with Parameters

    
    function functionName($variable, $variable..$variable)
    {
      code to be executed;
    }

    Let's put these syntax into real use.

    Following is the example showing how to create a function with parameters.

    Example : Create Function with Parameters

    
    <?php 
    function codealways($message) {
    echo "Code Always $message";
    }
    // call the function
    codealways("To Become Best Programmer."); 
    ?>

    This will produce the following result −

    Result

    
    Code Always To Become Best Programmer.

    PHP Functions - Returning values

    To let a function return a value, use the return statement.

    Example : Functions - Returning values

              
    <?php 
    function sum(int $x, int $y) {
     $z = $x + $y;
      return $z;
     } 
     echo "10 + 10 = " . sum(10, 10) . "<br>";
     echo "20 + 20 = " . sum(20, 20) . "<br>";
     echo "30 + 30 = " . sum(30, 30);
    ?>
        
    

    This will produce the following result −

    Result

    
    10 + 10 = 20
    20 + 20 = 40
    30 + 30 = 60